﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Basic_sprite_code
{
    class WindDemon:Sprite
    {
        public Apprentice summoner;
        public bool guard;
        public bool slain;
        public bool attacking;
        public int animCounter;
        public float deathChance;

        public Vector2 vel;

        public Texture2D attack1;
        public Texture2D attack2;

        public WindDemon(DarkLordsApprentice g, Apprentice a, Vector2 position, bool isGuard, int magicBoost)
            : base(g, "WindDemon1", position, new Vector2(100, 150))
        {
            summoner = a;
            guard = isGuard;
            slain = false;
            attacking = false;
            animCounter = 0;
            deathChance = 0.20f - 0.20f * (magicBoost / 100f);

            Random rand = new Random();
            vel = new Vector2(0,0);
            vel.X = 4.0f * (float)rand.NextDouble();
            vel.Y = 4 - vel.X;

            attack1 = game.Content.Load<Texture2D>("WindDemonAttack1");
            attack2 = game.Content.Load<Texture2D>("WindDemonAttack2");
        }

        public override void Update(GameTime gameTime)
        {
            if (slain)
            {
                if (animCounter > 130)
                {
                    game.Components.Remove(this);
                    summoner.currWindDemons--;
                }
                else if (animCounter > 100)
                {
                    image = game.Content.Load<Texture2D>("WindDemonSlain");
                }
                else
                {
                    if (!guard)
                    {
                        float posScale = (position.Y - 150.0f) / 300.0f;
                        if (position.X < 100 || position.X > 700)
                        {
                            vel.X *= -1;
                        }
                        if (position.Y < 150 || position.Y > 400)
                        {
                            vel.Y *= -1;
                        }
                        this.position.X += 2 * vel.X;
                        this.position.Y += vel.Y;
                        scale = 0.25f + posScale * 0.75f;
                    }

                    if (animCounter % 10 == 0 && image != attack1)
                    {
                        image = attack1;
                    }
                    else if (animCounter % 10 == 0)
                    {
                        image = attack2;
                    }
                }
                animCounter++;
            }
            else if (attacking)
            {
                if (animCounter > 300)
                {
                    attacking = false;
                    animCounter = 0;
                }
                else if (animCounter % 10 == 0 && image != attack1)
                {
                    image = attack1;
                }
                else if (animCounter % 10 == 0)
                {
                    image = attack2;
                }
                animCounter++;

                if (!guard)
                {
                    float posScale = (position.Y - 150.0f) / 300.0f;
                    if (position.X < 100 || position.X > 700)
                    {
                        vel.X *= -1;
                    }
                    if (position.Y < 150 || position.Y > 400)
                    {
                        vel.Y *= -1;
                    }
                    this.position.X += 2 * vel.X;
                    this.position.Y += vel.Y;
                    scale = 0.25f + posScale * 0.75f;
                }

            }
            else 
            {
                switch (animCounter)
                {
                    case 0:
                        image = game.Content.Load<Texture2D>("WindDemon1");
                        animCounter++;
                        break;
                    case 20:
                        image = game.Content.Load<Texture2D>("WindDemon2");
                        animCounter++;
                        break;
                    case 40:
                        image = game.Content.Load<Texture2D>("WindDemon3");
                        animCounter++;
                        break;
                    case 60:
                        image = game.Content.Load<Texture2D>("WindDemon2");
                        animCounter++;
                        break;
                    case 80:
                        animCounter = 0;
                        break;
                    default:
                        animCounter++;
                        break;
                }



                if (guard)
                {
                    scale = 1.0f;
                }
                else
                {
                    float posScale = (position.Y - 150.0f) / 300.0f;
                    if (position.X < 100 || position.X > 700)
                    {
                        vel.X *= -1;
                    }
                    if (position.Y < 150 || position.Y > 400)
                    {
                        vel.Y *= -1;
                    }
                    this.position.X += 2*vel.X;
                    this.position.Y += vel.Y;
                    scale = 0.25f + posScale * 0.75f;
                }
            }
            
            base.Update(gameTime);
        }
    }
}
